//
//  FoodJsCell.swift
//  NeusoftSchool
//
//  Created by 张瑞 on 2017/4/19.
//  Copyright © 2017年 李狗蛋. All rights reserved.
//

import UIKit

class FoodJsCell: UITableViewCell {
    
    //菜品名称
    var title: UILabel!
    //菜品介绍
    var foodIntroduce: UILabel!
    
    var food: ROFoodModel! {
        didSet(newValue) {
            
            foodIntroduce.text = food.intro as String;
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        
        //标题
        title = UILabel()
        title.text = "菜品名称"
        title.font = UIFont.systemFont(ofSize: 18)
        title.textColor = UIColor.blue
        self.contentView.addSubview(title)
        
        //课程介绍正文
        foodIntroduce = UILabel()
        foodIntroduce.font = UIFont.systemFont(ofSize: 16)
        foodIntroduce.textColor = UIColor.white
        
        //显示多行
        foodIntroduce.numberOfLines = 0
        self.contentView.addSubview(foodIntroduce)
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        title.frame = CGRect(x: 0, y: 0,width: UIScreen.main.bounds.size.width-16, height: 20)
        title.textAlignment = NSTextAlignment.center
        
        let cix: CGFloat = 8
        let ciy: CGFloat = 28
        let ciw: CGFloat = UIScreen.main.bounds.size.width-16
        var cih: CGFloat = 0
        //
        if food != nil {
            let foodIntroduce: NSString = food.intro
            
            let attributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 14)]
            let option = NSStringDrawingOptions.usesLineFragmentOrigin
            let text: NSString = NSString(cString: foodIntroduce.cString(using: String.Encoding.utf8.rawValue)!, encoding: String.Encoding.utf8.rawValue)!
            let intrRect: CGRect = text.boundingRect(with: CGSize(width: UIScreen.main.bounds.width, height: CGFloat(MAXFLOAT)), options: option, attributes: attributes, context: nil)
            //计算正文面积
            cih = intrRect.size.height;
            
        }
        foodIntroduce.frame = CGRect(x: cix, y: ciy, width: ciw, height: cih)
        foodIntroduce.backgroundColor = UIColor.purple
    }
    
    class func cellHeightWithPostinModel(_ food: ROFoodModel) -> CGFloat{
        
        var cih: CGFloat = 0
        
        let foodIntroduce: NSString = food.intro
        
        let attributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 14)]
        let option = NSStringDrawingOptions.usesLineFragmentOrigin
        let text: NSString = NSString(cString: foodIntroduce.cString(using: String.Encoding.utf8.rawValue)!, encoding: String.Encoding.utf8.rawValue)!
        let intrRect: CGRect = text.boundingRect(with: CGSize(width: UIScreen.main.bounds.width, height: CGFloat(MAXFLOAT)), options: option, attributes: attributes, context: nil)
        cih = intrRect.size.height;
        return cih+30+16
        
        
        
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
